package com.global.college.ui.fragment.training;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.global.college.R;
import com.global.college.base.baselist.BaseViewHolder;
import com.global.college.base.baselist.CommListFragment;
import com.global.college.base.baselist.CommRecyclerAdapter;
import com.global.college.constants.Constant;
import com.global.college.data.net.MyTrainListBean;
import com.global.college.ui.activity.TrainDetailBoughtActivity;
import com.global.college.ui.interfaces.ITrainingView;
import com.global.college.ui.presenter.TrainingPresenter;
import com.global.college.utils.StringUtils;
import com.global.college.widget.FrescoImageView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的训练营
 */
public class TrainingCampFragment extends CommListFragment implements ITrainingView {

    private int page = Constant.page;
    private int pageSize = Constant.pageSize;
    private List<MyTrainListBean.MyTrainBean> mData = new ArrayList<>();
    private static final String ARG_PARAM1 = "type";
    private String type;
    private TrainingPresenter trainingPresenter;

    public TrainingCampFragment() {
    }

    public static TrainingCampFragment newInstance(String type) {
        TrainingCampFragment fragment = new TrainingCampFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString(ARG_PARAM1);
        }
        trainingPresenter = new TrainingPresenter(mActivity);
        trainingPresenter.setTrainingView(this);
    }

    @Override
    public void fetchData() {
    }

    @Override
    protected void getData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        //type  0.全部 1.已购买 2.转卖中 3.转卖成功
        page = 1;
        getData(type);
    }

    @Override
    protected CommRecyclerAdapter setAdapter() {
        return new CommRecyclerAdapter() {
            @Override
            protected void onBindView(BaseViewHolder holder, final int position) {
                MyTrainListBean.MyTrainBean trainBean = mData.get(position);
                FrescoImageView fivCover = holder.getView(R.id.fiv_cover);
                TextView tvTitle = holder.getView(R.id.tv_title);
                TextView tvStatusTag = holder.getView(R.id.tv_status_tag);
                TextView tvStudyTime = holder.getView(R.id.tv_study_time);
                TextView tvStudyStatus = holder.getView(R.id.tv_study_status);
                TextView tvStudyProgress = holder.getView(R.id.tv_study_progress);
                TextView tvDetail = holder.getView(R.id.tv_detail);
                TextView tvContinueStudy = holder.getView(R.id.tv_continue_study);
                fivCover.setImageURI(trainBean.getCover());
                tvTitle.setText(trainBean.getName());
                if (trainBean.getClassState() == 0) {
                    tvStatusTag.setText("更新中");
                } else {
                    tvStatusTag.setText("已完结");
                }
                tvStudyTime.setText("学习时长：" + trainBean.getStudyTime() / 60 + "分钟");
                if (trainBean.getStudySpeed() == 1) {
                    tvContinueStudy.setVisibility(View.GONE);
                    tvDetail.setVisibility(View.VISIBLE);
                    tvStudyStatus.setTextColor(Color.parseColor("#212121"));
                    tvStudyStatus.setText("已学完");
                } else {
                    tvContinueStudy.setVisibility(View.VISIBLE);
                    tvDetail.setVisibility(View.GONE);
                    tvStudyStatus.setTextColor(Color.parseColor("#f63042"));
                    tvStudyStatus.setText("已学" + String.format("%.1f", (trainBean.getStudySpeed() * 100)) + "%");
                }
                if (!StringUtils.isEmpty(trainBean.getVideoName())){
                    tvStudyProgress.setText("学至：" + trainBean.getVideoName());
                }
                tvDetail.setOnClickListener(v -> mActivity.startActivity(new Intent(mActivity, TrainDetailBoughtActivity.class)
                        .putExtra("trainId", trainBean.getTrainingId())));
                tvContinueStudy.setOnClickListener(v -> mActivity.startActivity(new Intent(mActivity, TrainDetailBoughtActivity.class)
                        .putExtra("trainId", trainBean.getTrainingId())));
            }

            @Override
            protected int getLayoutID(int position) {
                return R.layout.item_common_traning;
            }

            @Override
            public int getItemCount() {
                return mData.size();
            }
        };
    }


    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        getData(type);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        getData(type);
    }

    private void getData(String type) {
        trainingPresenter.getTrainingList(page, pageSize, Integer.valueOf(type));
    }

    @Override
    public void onGetTrainingListSuccess(MyTrainListBean model) {
        if (null != model) {
            if (page == 1) {
                refreshFinish();
                mData.clear();
                if (null != model.getList() && model.getList().size() > 0) {
                    mData.addAll(model.getList());
                } else {
                    setEmpty(getString(R.string.cube_ptr_not_trainning_data), R.drawable.icon_empty);
                }
            } else {
                finishLoadmore();
                if (null != model.getList() && model.getList().size() > 0)
                    mData.addAll(model.getList());
            }
            if (null != model.getList() && model.getList().size() < pageSize) {
                finishLoadmoreWithNoMoreData();
            }

            if (mAdapter != null) {
                mAdapter.notifyDataSetChanged();
            }

        } else {
            setEmpty(getString(R.string.cube_ptr_not_trainning_data), R.drawable.icon_empty);
        }
    }

    @Override
    public void onGetTrainingListFail(String error) {

    }

    @Override
    public void onGetTrainingDetailSuccess(Object smsModel) {

    }

    @Override
    public void onGetTrainingDetailFail(String error) {

    }
}